# Name: [Blog Comment Test]
# By: [Wyatt Preul]
# [This will test whether try to post a comment when it is disabled, then again after enabling comments]
# [in the sample blog]
# Maintenance:
#   [9/21/2006] : [WLP] : Created
#

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..') if $0 == __FILE__
require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'

#_replace_Template_with_the_short_name_of_your_test_case
class TC_BlogComment_test < Test::Unit::TestCase
    
    def startup
      $Name = randomChar(7)
      $SiteAddress = randomChar(10)
      $CommentBody = randomChar(30)
      @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
      ie.link(:text, 'Sign in').click
      assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
      ie.text_field(:id, /username$/).value = $ADMIN_USER
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
      assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
      ie.link(:id, /loginButton$/).click
      test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
      assert(ie.link(:text, /Control Panel/).exists?)
      ie.link(:text, /Control Panel/).click
      assert(ie.contains_text(/Community Server Announcements/))
      assert(ie.link(:text, /Administration/).exists?)
      ie.link(:text, /Administration/).click
      assert(ie.contains_text(/Community Server Announcements/))
    end
    
    def test_07_verifyControlPanelTabBarLinks
      assert_equal $SITE_URL + 'controlpanel/default.aspx', ie.link(:text, 'Dashboard').href
      assert_equal $SITE_URL + 'controlpanel/blogs', ie.link(:text, 'My Blogs').href
      assert_equal $SITE_URL + 'controlpanel/photos/', ie.link(:text, 'My Photos').href
      assert_equal $SITE_URL + 'controlpanel/files', ie.link(:text, 'My Files').href
      assert_equal $SITE_URL + 'controlpanel/moderation', ie.link(:text, 'Forums Moderation').href
      assert_equal $SITE_URL + 'controlpanel/Setup.aspx', ie.link(:text, 'Administration').href
      assert_equal $SITE_URL + 'controlpanel/Reporting/default.aspx', ie.link(:text, 'Reporting').href
    end
    
    def test_08_navigateToPostSettings
      ie.goto($SITE_URL + 'controlpanel/Settings/Posts.aspx')
      assert(ie.text_field(:id, /SearchPerPage$/).exists?)
      assert(ie.text_field(:id, /TopPostersToDisplay$/).exists?)
      assert(ie.link(:id, /logout$/).exists?)
      assert(ie.link(:text, 'Help').exists?)
      assert(ie.link(:text, 'Inbox').exists?)
      assert(ie.link(:text, 'Exit Control Panel and Return to Site').exists?)
      assert(ie.link(:text, 'Dashboard').exists?)
      assert(ie.link(:text, 'My Blogs').exists?)
      assert(ie.link(:text, 'My Photos').exists?)
      assert(ie.link(:text, 'My Files').exists?)
      assert(ie.link(:text, 'Forums Moderation').exists?)
      assert(ie.link(:text, 'Administration').exists?)
      assert(ie.link(:text, 'Reporting').exists?)
      assert(ie.link(:id, /Helpicon12$/).exists?)
      assert(ie.link(:id, /Helpicon8$/).exists?)
      assert(ie.link(:id, /Helpicon2$/).exists?)
      assert(ie.link(:id, /Helpicon14$/).exists?)
      assert(ie.link(:id, /Helpicon10$/).exists?)
      assert(ie.link(:id, /Helpicon11$/).exists?)
      assert(ie.link(:id, /Helpicon1$/).exists?)
      assert(ie.link(:id, /Helpicon5$/).exists?)
      assert(ie.link(:text, 'Telligent').exists?)
    end
    
    def test_09_enableAnonymousPosting
      assert(ie.radio(:id, /AnonymousUserPosting_0$/).exists?)
      assert(ie.radio(:id, /AnonymousUserPosting_1$/).exists?)
      ie.radio(:id, /AnonymousUserPosting_0$/).set
      ie.radio(:id, /AnonymousUserPosting_1$/).clear

      assert(ie.link(:id, /SaveButton$/).exists?)
      ie.link(:id, /SaveButton$/).click
      
      assert(ie.contains_text('Settings successfully saved.'))
    end
    
    def test_10_navigateToBlogProperties
      ie.goto($SITE_URL + 'ControlPanel/Blogs/PostOptions.aspx')
      
      assert(ie.link(:text, 'admin').exists?)
      assert(ie.link(:id, /logout$/).exists?)
      assert(ie.link(:text, 'Help').exists?)
      assert(ie.link(:text, 'Inbox').exists?)
      assert(ie.link(:text, 'Exit Control Panel and Return to Site').exists?)
      assert(ie.link(:text, 'Dashboard').exists?)
      assert(ie.link(:text, 'My Blogs').exists?)
      assert(ie.link(:text, 'My Photos').exists?)
      assert(ie.link(:text, 'My Files').exists?)
      assert(ie.link(:text, 'Forums Moderation').exists?)
      assert(ie.link(:text, 'Administration').exists?)
      assert(ie.link(:text, 'Reporting').exists?)
      assert(ie.link(:text, 'Title, Description, and News').exists?)
      assert(ie.link(:text, 'Change How My Blog Looks').exists?)
      assert(ie.link(:text, 'My Email Settings').exists?)
      assert(ie.link(:text, 'Default Post Settings').exists?)
      assert(ie.link(:text, 'Advanced Post Settings').exists?)
      assert(ie.link(:text, 'Syndication Settings').exists?)
      assert(ie.link(:text, 'Spam, Ping and Cross-Posting').exists?)
    end
    
    def test_11_enableAnonymousCommentsOnBlog
      assert(ie.radio(:id, /EnableAnonymousReplies_0$/).exists?)
      assert(ie.radio(:id, /EnableAnonymousReplies_1$/).exists?)
      
      ie.radio(:id, /EnableAnonymousReplies_0$/).set
      ie.radio(:id, /EnableAnonymousReplies_1$/).clear
      
      assert(ie.link(:id, /SaveButton$/).exists?)
      ie.link(:id, /SaveButton$/).click
      
      assert(ie.contains_text('Your options have been successfully saved'))
    end
    
    def test_12_verifyLogoutLinkExists
      assert_equal $SITE_URL + 'logout.aspx', ie.link(:id, /logout/).href
    end
    
    def test_13_navigateToLogout
      ie.link(:id, /logout/).click
      assert(ie.contains_text("Return home"))
    end
    
    def test_14_verifyLogoutPageLinks
      assert(ie.link(:url, $SITE_URL).exists?)
      assert(ie.link(:text, 'Sign in').exists?)
      assert(ie.link(:text, 'Join').exists?)
      assert(ie.link(:text, 'Help').exists?)
      assert(ie.button(:id, /TitleBarSearchButton$/).exists?)
      assert(ie.link(:text, 'Home').exists?)
      assert(ie.link(:text, 'Blogs').exists?)
      assert(ie.link(:text, 'Forums').exists?)
      assert(ie.link(:text, 'Photos').exists?)
      assert(ie.link(:text, 'Downloads').exists?)
      assert(ie.link(:text, 'Sign in').exists?)
      assert(ie.link(:text, 'Return home').exists?)
    end
    
    def test_15_navigateToBlogPost
      assert(ie.link(:text, 'Blogs').exists?)
      ie.link(:text, 'Blogs').click

      assert(ie.link(:text, 'Welcome to Community Server Blogs!').exists?)
      ie.link(:text, 'Welcome to Community Server Blogs!').click
      
      assert(ie.contains_text("Welcome to Community Server Blogs"))
    end
    
    def test_16_leaveAComment
      assert(ie.contains_text("Leave a Comment")) 

      assert(ie.text_field(:id, /tbName$/).exists?)
      ie.text_field(:id, /tbName$/).set($Name)
      assert(ie.text_field(:id, /tbName$/).verify_contains($Name))
      assert(ie.text_field(:id, /tbUrl$/).exists?)
      ie.text_field(:id, /tbUrl$/).set($SiteAddress)
      assert(ie.text_field(:id, /tbUrl$/).verify_contains($SiteAddress))
      assert(ie.text_field(:id, /tbComment$/).exists?)
      ie.text_field(:id, /tbComment$/).set($CommentBody)
      assert(ie.text_field(:id, /tbComment$/).verify_contains($CommentBody))
      
      assert(ie.link(:text, "Add").exists?)
      ie.link(:text, "Add").click
       
      assert(ie.contains_text("Thanks for sharing your feedback"))
    end
    
    def test_17_navigateToLogin
      ie.link(:text, 'Sign in').click
      assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_18_populateLoginForm
      ie.text_field(:id, /username$/).set($ADMIN_USER)
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).set($ADMIN_PASSWORD)
      assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_19_submitLoginForm
      ie.link(:id, /loginButton$/).click
      assert(ie.contains_text("Thanks for sharing your feedback"))
    end
    
    def test_20_postRegisteredComment
      $SiteAddress = randomChar(2)
      $CommentBody = randomChar(50)
      
      assert(ie.text_field(:id, /tbUrl$/).exists?)
      ie.text_field(:id, /tbUrl$/).set($SiteAddress)
      assert(ie.text_field(:id, /tbUrl$/).verify_contains($SiteAddress))
      assert(ie.text_field(:id, /tbComment$/).exists?)
      ie.text_field(:id, /tbComment$/).set($CommentBody)
      assert(ie.text_field(:id, /tbComment$/).verify_contains($CommentBody))
      
      assert(ie.link(:id, /btnSubmit$/).exists?)
      ie.link(:id, /btnSubmit$/).click
      
      assert(ie.contains_text("Thanks for sharing your feedback"))
    end
    
   def test_21_navigateToPostSettings
      ie.goto($SITE_URL + 'controlpanel/Settings/Posts.aspx')
      assert(ie.text_field(:id, /SearchPerPage$/).exists?)
      assert(ie.text_field(:id, /TopPostersToDisplay$/).exists?)
      assert(ie.link(:id, /logout$/).exists?)
      assert(ie.link(:text, 'Help').exists?)
      assert(ie.link(:text, 'Inbox').exists?)
      assert(ie.link(:text, 'Exit Control Panel and Return to Site').exists?)
      assert(ie.link(:text, 'Dashboard').exists?)
      assert(ie.link(:text, 'My Blogs').exists?)
      assert(ie.link(:text, 'My Photos').exists?)
      assert(ie.link(:text, 'My Files').exists?)
      assert(ie.link(:text, 'Forums Moderation').exists?)
      assert(ie.link(:text, 'Administration').exists?)
      assert(ie.link(:text, 'Reporting').exists?)
      assert(ie.link(:id, /Helpicon12$/).exists?)
      assert(ie.link(:id, /Helpicon8$/).exists?)
      assert(ie.link(:id, /Helpicon2$/).exists?)
      assert(ie.link(:id, /Helpicon14$/).exists?)
      assert(ie.link(:id, /Helpicon10$/).exists?)
      assert(ie.link(:id, /Helpicon11$/).exists?)
      assert(ie.link(:id, /Helpicon1$/).exists?)
      assert(ie.link(:id, /Helpicon5$/).exists?)
      assert(ie.link(:text, 'Telligent').exists?)
    end
       
    def test_22_rollbackToDefaultSetting
      assert(ie.radio(:id, /AnonymousUserPosting_0$/).exists?)
      assert(ie.radio(:id, /AnonymousUserPosting_1$/).exists?)
      ie.radio(:id, /AnonymousUserPosting_1$/).set
      ie.radio(:id, /AnonymousUserPosting_0$/).clear

      assert(ie.link(:id, /SaveButton$/).exists?)
      ie.link(:id, /SaveButton$/).click
      
      assert(ie.contains_text('Settings successfully saved.'))
    end
    
    def test_23_logoutAndTryCommenting
      test_12_verifyLogoutLinkExists
      test_13_navigateToLogout
      test_14_verifyLogoutPageLinks
      test_15_navigateToBlogPost
      
      assert(!ie.contains_text("Leave a Comment"))
      
      ie.goto($SITE_URL)
    end
    
    def test_99_closeIE
      ie.close
    end
    
end